#include "QtRobot.h"
#include <QPainter>
#include "QtBlendToEffect.h"
#include "QtSequentialAnimationEffect.h"
#include "QtScaleToEffect.h"
#include "QtBlinkEffect.h"

QtRobot::QtRobot(const std::string& type, QtSprite *parent): QtPixmapSprite(parent), Robot(type), duration(1000)
{
	SetDepth(5);
	laserBeam = new QtLineSprite();
	laserBeam->SetColor(Qt::red);
	laserBeam->SetAlpha(0);
	laserBeam->SetDepth(10);
	laserBeam->SetLineWidth(3);

	AddSprite(laserBeam);

	SetResource("robot", "robot");
	AddResourceSpecification("robot", type);
}

QtRobot::~QtRobot()
{
	if (laserBeam)
	{
		delete laserBeam;
		laserBeam = 0;
	}
}

void QtRobot::SetMovementDistance(unsigned int distance)
{
	movementDistance = distance;
}

void QtRobot::SetLaserDestination(const QPointF& pos)
{
	laserDestination = pos;
}

void QtRobot::Shoot()
{
	if (!laserBeam)
		return;

	laserBeam->SetLine(Position(), laserDestination);

	QtSequentialAnimationEffect seq;

	QtBlendToEffect effect;

	effect.SetDuration(100);
	effect.SetTarget(laserBeam);
	effect.SetEndAlpha(1);

	seq.AddEffect(&effect);

	effect.SetDuration(100);
	effect.SetTarget(laserBeam);
	effect.SetEndAlpha(0);

	seq.AddEffect(&effect);

	AddEffect(&seq);

	Start();
}

void QtRobot::MoveLeft()
{
	QPointF position = Position();

	position.setX( position.x() - movementDistance );
	MoveTo(position, duration);

	Start();
}

void QtRobot::MoveRight()
{
	QPointF position = Position();

	position.setX( position.x() + movementDistance );
	MoveTo(position, duration);

	Start();
}

void QtRobot::MoveUp()
{
	QPointF position = Position();

	position.setY( position.y() - movementDistance );
	MoveTo(position, duration);

	Start();
}

void QtRobot::MoveDown()
{
	QPointF position = Position();

	position.setY( position.y() + movementDistance );
	MoveTo(position, duration);

	Start();
}

void QtRobot::TurnLeft()
{
	qreal newAngle = Angle() - 90;
	if (newAngle < 360)
		newAngle += 360;

	RotateLeftTo(newAngle, duration);

	Start();
}

void QtRobot::TurnRight()
{
	qreal newAngle = Angle() + 90;
	if (newAngle > 360)
		newAngle -= 360;

	RotateRightTo(newAngle, duration);

	Start();
}

void QtRobot::Fall()
{
	ScaleTo(1,1, 500);

	Start();
}

void QtRobot::Spawn()
{
	SetPosition(spawnPosition);
	ScaleTo(Width(), Height(), 500);

	Start();
}

void QtRobot::SetSpawnPosition(const QPointF& pos)
{
	spawnPosition = pos;
}

void QtRobot::Hit()
{
	QtBlinkEffect effect;

	effect.SetDuration(duration);
	effect.SetBlinkDuration(90);
	effect.SetTarget(this);

	AddEffect(&effect);

	Start();
}




